package com.zhangdi.dwtools.security;

import io.jsonwebtoken.*;

import java.util.Date;
import java.util.Map;

/**
 * Created by zhangdi on 2020/07/31 14:46:21.
 */
public class JWTUtils {
    private final static String base64key = "hgEdG2PkzKSnnuMQ9dN00j9vqtRKQFEUGgWeKjQsiybtFFAF3dpmAm6HvBvyfJqD";

    private final static long EXPIRE_TIME = 2 * 60 * 1000;//令牌有效期 默认10分钟
    private final static long REFRESH_TIME = 60 * 60 * 1000;//刷新时间 默认1小时

    public static String getAccessToken(long uid) {
        Date now = new Date();
        return Jwts.builder().setId(uid + "").
                setIssuedAt(now).
                setExpiration(new Date(now.getTime() + EXPIRE_TIME)).
                signWith(SignatureAlgorithm.HS512, base64key).
                compressWith(CompressionCodecs.GZIP).
                compact();
    }

    public static String getAccessToken(String uid) {
        Date now = new Date();
        return Jwts.builder().setId(uid).
                setIssuedAt(now).
                setExpiration(new Date(now.getTime() + EXPIRE_TIME)).
                signWith(SignatureAlgorithm.HS512, base64key).
                compressWith(CompressionCodecs.GZIP).
                compact();
    }

    public static String getRefreshToken(long uid) {
        Date now = new Date();
        return Jwts.builder().setId(uid + "").
                setIssuedAt(now).
                setExpiration(new Date(now.getTime() + REFRESH_TIME)).
                signWith(SignatureAlgorithm.HS512, base64key).
                compressWith(CompressionCodecs.GZIP).
                compact();
    }

    public static String getRefreshToken(String uid) {
        Date now = new Date();
        return Jwts.builder().setId(uid).
                setIssuedAt(now).
                setExpiration(new Date(now.getTime() + REFRESH_TIME)).
                signWith(SignatureAlgorithm.HS512, base64key).
                compressWith(CompressionCodecs.GZIP).
                compact();
    }

    public static boolean isExpired(String token) {
        try {
            Claims claims = Jwts.parser().setSigningKey(base64key).parseClaimsJws(token).getBody();
            if (claims == null) {
                return true;
            }
            return claims.getExpiration().before(new Date());
        } catch (Exception e) {
            return true;
        }
    }

    public static boolean aboutExpire(String token){
        try {
            Claims claims = Jwts.parser().setSigningKey(base64key).parseClaimsJws(token).getBody();
            if (claims == null) {
                return true;
            }

            double now = new Date().getTime()*0.001;
            double expire = claims.getExpiration().getTime()*0.001;
            double near = now / expire;

            return near >= 0.75;
        } catch (Exception e) {
            return true;
        }
    }

    public static String refreshExpired(String token) {
        Date now = new Date();
        try {
            Claims claims = Jwts.parser().setSigningKey(base64key).parseClaimsJws(token).getBody();
            if (claims == null) {
                return null;
            }
            JwtBuilder builder = Jwts.builder();
            for (Map.Entry<String, Object> entry : claims.entrySet()) {
                builder.claim(entry.getKey(), entry.getValue());
            }

            return builder.setExpiration(new Date(now.getTime() + EXPIRE_TIME)).
                    signWith(SignatureAlgorithm.HS512, base64key).
                    compressWith(CompressionCodecs.GZIP).
                    compact();
        } catch (Exception e) {
            return null;
        }
    }

    public static String getId(String token) {
        try {
            Jws<Claims> claims = Jwts.parser().setSigningKey(base64key).parseClaimsJws(token);
            return claims.getBody().getId();
        } catch (Exception ignored) {
        }
        return null;
    }
}
